<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * 师傅点管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['ops']=array(
	'1'=>'订单管理',
	'2'=>'接收订单通知',
	'3'=>'服务员通知',
	'4'=>'叫号管理',
);
if($_RQ['op']=='list'){
	$params=array('merchid'=>$merchid);
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','title','desc','store_id','merchid');
	$list=pdo_getslice('jy_shifu_area',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$item){
		$item['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$item['store_id'],'uniacid'=>$_SESSION['uniacid']),'title');
		$item['storetitle']=$item['storetitle']?:'系统平台';
		$member=pdo_getall('jy_shifu_member',array('group_id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'mid');
		$item['usernum']=count($member);
		$item['orderstatus2']=0;
		$item['orderstatus3']=0;
		$item['noworderstatus2']=0;
		$item['noworderstatus3']=0;
		foreach($member as $mv){
			$item['orderstatus2'] +=pdo_count('jy_shifu_shoulog',array('mid'=>$mv['mid'],'status'=>2,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),'id');
			$item['orderstatus3'] +=pdo_count('jy_shifu_shoulog',array('mid'=>$mv['mid'],'status'=>1,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),'id');
			$item['noworderstatus2'] +=pdo_count('jy_shifu_shoulog',array('mid'=>$mv['mid'],'createtime'=>$daytime,'status'=>2,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),'id');
			$item['noworderstatus3'] +=pdo_count('jy_shifu_shoulog',array('mid'=>$mv['mid'],'createtime'=>$daytime,'status'=>1,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),'id');
			if($item['merchid']){
				$item['merchname']=pdo_getcolumn('jy_merch_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']),'merchname');
			}else{
				$item['merchname']='平台';
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'师傅点类型列表'),
			array('op'=>'edit','title'=>'添加师傅点类型'),
		),
		'list'=>array('data'=>'list','tip'=>'【类型理解】：每个门店也许有多少个区域分布，不同区域由不同的师傅负责！','list'=>array(
			array('name'=>'title','title'=>'网点名称','type'=>'string'),
			array('name'=>'storetitle','title'=>'所属门店','type'=>'string'),
			array('name'=>'merchname','title'=>'所属商户','type'=>'string'),
			array('name'=>'usernum','title'=>'师傅人数','type'=>'string'),
			array('type'=>'img_string','title'=>'总完成/师傅中订单',
			'data'=>array(
				array('title'=>'完成订单数：','name'=>'orderstatus2'),
				array('title'=>'师傅中订单数：','name'=>'orderstatus3')
				)
			),
			array('type'=>'img_string','title'=>'今日完成/师傅中订单',
			'data'=>array(
				array('title'=>'完成订单数：','name'=>'noworderstatus2'),
				array('title'=>'师傅中订单数：','name'=>'noworderstatus3')
				)
			),
			array('name'=>'desc','title'=>'备注','type'=>'string'),
		)),
		'search_one'=>array('name'=>'title'),
	);
	$return['htmls']=$htmls;
	
	exi($return);
}

if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_shifu_area',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['isstore']=$item['store_id']?1:0;
	}else{
		$item=array();
	}
	$item['pcd']=json_decode($item['pcd'],true);
	$item['lat_lng']=json_decode($item['lat_lng'],true);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回师傅点列表'),
		),
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'网点名称','name'=>'title','help'=>''),
					array('type'=>'switch','title'=>'门店区域?','name'=>'isstore','help'=>'不选择门店，则表示系统师傅区域！'),
					array('type'=>'select','title'=>'所属门店','name'=>'store_id','help'=>'','options'=>'stores','help'=>'不选择门店，则表示系统师傅区域！'),
					array('type'=>'pcd','title'=>'师傅区域','name'=>'pcd','help'=>''),
					array('type'=>'string','title'=>'师傅详细地址','name'=>'address','help'=>''),
					array('type'=>'lat_lng','title'=>'师傅坐标','name'=>'lat_lng','help'=>''),
					array('type'=>'text','title'=>'备注','name'=>'desc','help'=>''),
				)
			),
		)
	);
	$return['stores']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']));
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'store_id'=>$postdata['isstore']?intval($postdata['store_id']):0,
		'desc'=>trim($postdata['desc']),
		'pcd'=>ijson_encode($postdata['pcd']),
		'address'=>trim($postdata['address']),
		'lat_lng'=>ijson_encode($postdata['lat_lng']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_shifu_area',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$merchid;
		pdo_insert('jy_shifu_area',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_shifu_area',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}